﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Visuals.Forms;

namespace STCommon.View
{
    public partial class LineChartWithButtons : UserControl
    {
        public LineChartWithButtons()
        {
            InitializeComponent();

            SaveImageButton.CenterImage = CommonResources.Images.Save16;
            ZoomToContentButton.CenterImage = Resources.Resources.ZoomToContent;
            ZoomInButton.CenterImage = CommonResources.Images.ZoomIn16;
            ZoomOutButton.CenterImage = CommonResources.Images.ZoomOut16;
            ExtraChartsButton.CenterImage = Resources.Resources.MoreCharts;
            ShowZonesButton.CenterImage = Resources.Resources.ShowZones;
        }

        private void ZoomInButton_Click(object sender, EventArgs e)
        {
            ChartControl.ZoomIn();
        }

        private void ZoomOutButton_Click(object sender, EventArgs e)
        {
            ChartControl.ZoomOut();
        }

        private void ZoomToContentButton_Click(object sender, EventArgs e)
        {
            ChartControl.AutozoomToData(true);
        }

        private void SaveImageButton_Click(object sender, EventArgs e)
        {
            SaveImageDialog dlg = new SaveImageDialog();

            dlg.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures).ToString() +
                           "Chart " + DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture);
            dlg.CanChangeImageSize = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Size imgSize = dlg.CustomImageSize;

                if (dlg.ImageSize != SaveImageDialog.ImageSizeType.Custom)
                {
                    imgSize = dlg.ImageSizes[dlg.ImageSize];
                }

                ChartControl.SaveImage(imgSize, dlg.FileName, dlg.ImageFormat);
            }
        }

        public void ThemeChaged(ITheme visualTheme)
        {
            ChartControl.ThemeChanged(visualTheme);
            ButtonsPanel.ThemeChanged(visualTheme);
            ButtonsPanel.BackColor = visualTheme.Window;
        }

        private void ExtraChartsButton_Click(object sender, EventArgs e)
        {
            if (ExtraChartsButtonClicked != null)
            {
                ExtraChartsButtonClicked(sender, e);
            }
        }

        private void ShowZonesButton_Click(object sender, EventArgs e)
        {
            if (ShowZonesButtonClicked != null)
            {
                ShowZonesButtonClicked(sender, e);
            }
        }

        [DisplayName("Show Save Image button")]
        public bool SaveImageButtonVisible
        {
            get { return SaveImageButton.Visible; }
            set { SaveImageButton.Visible = value; }
        }

        [DisplayName("Show Zoom to Content button")]
        public bool ZoomToContentButtonVisible
        {
            get { return ZoomToContentButton.Visible; }
            set { ZoomToContentButton.Visible = value; }
        }

        [DisplayName("Show Zoom In button")]
        public bool ZoomInButtonVisible
        {
            get { return ZoomInButton.Visible; }
            set { ZoomInButton.Visible = value; }
        }

        [DisplayName("Show Zoom Out button")]
        public bool ZoomOutButtonVisible
        {
            get { return ZoomOutButton.Visible; }
            set { ZoomOutButton.Visible = value; }
        }

        [DisplayName("Show Extra Charts button")]
        public bool ExtraChartsButtonVisible
        {
            get { return ExtraChartsButton.Visible; }
            set { ExtraChartsButton.Visible = value; }
        }

        [DisplayName("Show Zones button")]
        public bool ShowZonesButtonVisible
        {
            get { return ShowZonesButton.Visible; }
            set { ShowZonesButton.Visible = value; }
        }

        [DisplayName("Chart selectable")]
        public bool ChartSelectable
        {
            get { return ChartControl.AllowSelection; }
            set { ChartControl.AllowSelection = value; }
        }

        [Browsable(false)]
        public NonSelectableLineChart LineChart
        {
            get { return ChartControl; }
        }

        public event EventHandler ExtraChartsButtonClicked;
        public event EventHandler ShowZonesButtonClicked;
    }
}
